package com.daqsoft.controller.system;

import com.daqsoft.data.Group;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.dto.UserManageResultEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.system.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户组管理
 * @author 周麟东 on 9:37
 */
@RestController
@RequestMapping("groupManager")
@Verify(desc = "用户组管理")
public class GroupManageController {

    @Autowired
    private GroupService service;

    @RequestMapping("getAll")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取全部用户组")
    public GeneralResultEnum getAllGroup(){
        List<Map<String,Object>> list = new ArrayList<>();
        for (Group group : service.findAll()) {
            list.add(group.toMap());
        }
        return GeneralResultEnum.SUCCESS.setData(list).setDesc("获取用户组成功");
    }

    @RequestMapping("save")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "保存用户组")
    public GeneralResultEnum saveGroup(Group group , Integer leaderId){
        UserManageResultEnum result = service.save(group , leaderId);
        switch (result){
            case GROUP_NAME_REPEAT:
                return GeneralResultEnum.FAIL.setDesc("用户组名称与当前已有的重复");
            case LEVEL_ERR:
                return GeneralResultEnum.FAIL.setDesc("用户组等级不能小于0");
        }
        return GeneralResultEnum.SUCCESS.setData(result.getGroupData().toMap());
    }
    @RequestMapping("getGroup")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取某个用户组")
    public GeneralResultEnum getGroup(int id){
        return GeneralResultEnum.SUCCESS.setData(service.findOne(id).toMap()).setDesc("获取成功");
    }
    @RequestMapping("remove")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "删除用户组")
    public GeneralResultEnum removeGroup(int id){
        service.remove(id);
        return GeneralResultEnum.SUCCESS;
    }
}
